package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.util.SmsUtil;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author : 邓风来
 * @date : 2025/9/16 9:26
 */
@RestController
@RequestMapping("/sms")
public class SmsController {
    @Autowired
    private RedisService redisService;

    @PostMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
//        String code = UUID.randomUUID().toString().replace("-", "").substring(0,6);
        Integer rand = new Random().nextInt(900000) + 100000;
        String code = rand.toString();
        SmsUtil.sendSmsAli(phone,code);
        System.err.println("验证码:" + code);
        String key = RedisKeys.VERIFY_CODE.join(phone);
        // 将验证码存储到Redis中
        redisService.setCacheObject(key,code,RedisKeys.VERIFY_CODE.getTime(), TimeUnit.SECONDS);

        return R.ok();
    }
}
